package com.xbj.voa.common.wrapper;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.json.JSONUtil;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: 木火
 * @create: 2024-01-03
 * @description:抵御跨站脚本XSS攻击 配置XssFilter拦截所有请求,把请求传入次包装类进行转义
 **/
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if(!StrUtil.hasEmpty(value)){
            value = HtmlUtil.filter(value);
        }
        return value;
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if(values != null){
            for (int i = 0; i < values.length; i++) {
                if(!StrUtil.hasEmpty(values[i])){
                    values[i] = HtmlUtil.filter(values[i]);
                }
            }
        }
        return values;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> parameterMap = super.getParameterMap();
        Map<String, String[]> map = new LinkedHashMap<>();
        if(parameterMap != null){
            for (String key :
                    parameterMap.keySet()) {
                String[] values = parameterMap.get(key);
                for (int i = 0; i < values.length; i++) {
                    if(!StrUtil.hasEmpty(values[i])){
                        values[i] = HtmlUtil.filter(values[i]);
                    }
                }
                map.put(key,values);
            }
        }
        return map;
    }

    @Override
    public String getHeader(String name) {
        String value =  super.getHeader(name);
        if(!StrUtil.hasEmpty(value)){
            value = HtmlUtil.filter(value);
        }
        return value;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        InputStream inputStream = super.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        BufferedReader bufferedReader = new BufferedReader(reader);

        StringBuffer body = new StringBuffer();
        String line = bufferedReader.readLine();
        while (line != null){
            body.append(line);
            line = bufferedReader.readLine();
        }

        bufferedReader.close();
        reader.close();
        inputStream.close();

        Map<String,Object> map  = JSONUtil.parseObj(body.toString());
        Map<String,Object> resultMap = new HashMap<>(map.size());
        for (String key :
                map.keySet()) {
            Object val = map.get(key);
            if(val instanceof String){
                resultMap.put(key,HtmlUtil.filter(val.toString()));
            }else {
                resultMap.put(key,val);
            }
        }

        String str = JSONUtil.toJsonStr(resultMap);
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
        return new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }
        };
    }
}
